import { ipcMain } from 'electron'
import { IpcUtil } from '@xtail/electron'
import { IpcChannelNameConst } from '@common'
import {
  saveHostConnConfs,
  findHostConnConfs,
  removeHostConnConfsByIds,
  testConn
} from './host-conn-conf'
import { shellSessionOpen, shellSessionExec, shellSessionClose } from './shell-session'
import { SSHSessionOpen, SSHSessionExec, SSHSessionClose } from './ssh-session'
import { saveCmdConfs, findCmdConfs, removeCmdConfsByIds } from './cmd-conf'
import { saveScriptConfs, findScriptConfs, removeScriptConfsByIds } from './script-conf'
import {
  saveScriptNodeConfs,
  findScriptNodeConfs,
  removeScriptNodeConfsByIds,
  removeScriptNodeConfs
} from './script-node-conf'
import {
  saveScriptEdgeConfs,
  findScriptEdgeConfs,
  removeScriptEdgeConfsByIds,
  removeScriptEdgeConfs
} from './script-edge-conf'

export const ipcBounding = () => {
  // 服务器主机连接配置 CRUD
  ipcMain.handle(
    IpcChannelNameConst.saveHostConnConfs,
    IpcUtil.genMainIpcHandleListener(saveHostConnConfs)
  )
  ipcMain.handle(
    IpcChannelNameConst.findHostConnConfs,
    IpcUtil.genMainIpcHandleListener(findHostConnConfs)
  )
  ipcMain.handle(
    IpcChannelNameConst.removeHostConnConfsByIds,
    IpcUtil.genMainIpcHandleListener(removeHostConnConfsByIds)
  )
  // 服务器主机连接测试
  ipcMain.handle(IpcChannelNameConst.testConn, IpcUtil.genMainIpcHandleListener(testConn))
  // 本机终端会话
  ipcMain.handle(
    IpcChannelNameConst.shellSessionOpen,
    IpcUtil.genMainIpcHandleListener(shellSessionOpen)
  )
  ipcMain.handle(
    IpcChannelNameConst.shellSessionExec,
    IpcUtil.genMainIpcHandleListener(shellSessionExec)
  )
  ipcMain.handle(
    IpcChannelNameConst.shellSessionClose,
    IpcUtil.genMainIpcHandleListener(shellSessionClose)
  )
  // 服务器主机 SSH 会话
  ipcMain.handle(
    IpcChannelNameConst.SSHSessionOpen,
    IpcUtil.genMainIpcHandleListener(SSHSessionOpen)
  )
  ipcMain.handle(
    IpcChannelNameConst.SSHSessionExec,
    IpcUtil.genMainIpcHandleListener(SSHSessionExec)
  )
  ipcMain.handle(
    IpcChannelNameConst.SSHSessionClose,
    IpcUtil.genMainIpcHandleListener(SSHSessionClose)
  )
  // 命令配置 CRUD
  ipcMain.handle(IpcChannelNameConst.saveCmdConfs, IpcUtil.genMainIpcHandleListener(saveCmdConfs))
  ipcMain.handle(IpcChannelNameConst.findCmdConfs, IpcUtil.genMainIpcHandleListener(findCmdConfs))
  ipcMain.handle(
    IpcChannelNameConst.removeCmdConfsByIds,
    IpcUtil.genMainIpcHandleListener(removeCmdConfsByIds)
  )
  // 脚本配置 CRUD
  ipcMain.handle(
    IpcChannelNameConst.saveScriptConfs,
    IpcUtil.genMainIpcHandleListener(saveScriptConfs)
  )
  ipcMain.handle(
    IpcChannelNameConst.findScriptConfs,
    IpcUtil.genMainIpcHandleListener(findScriptConfs)
  )
  ipcMain.handle(
    IpcChannelNameConst.removeScriptConfsByIds,
    IpcUtil.genMainIpcHandleListener(removeScriptConfsByIds)
  )
  // 脚本节点配置 CRUD
  ipcMain.handle(
    IpcChannelNameConst.saveScriptNodeConfs,
    IpcUtil.genMainIpcHandleListener(saveScriptNodeConfs)
  )
  ipcMain.handle(
    IpcChannelNameConst.findScriptNodeConfs,
    IpcUtil.genMainIpcHandleListener(findScriptNodeConfs)
  )
  ipcMain.handle(
    IpcChannelNameConst.removeScriptNodeConfsByIds,
    IpcUtil.genMainIpcHandleListener(removeScriptNodeConfsByIds)
  )
  ipcMain.handle(
    IpcChannelNameConst.removeScriptNodeConfs,
    IpcUtil.genMainIpcHandleListener(removeScriptNodeConfs)
  )
  // 脚本节点边配置 CRUD
  ipcMain.handle(
    IpcChannelNameConst.saveScriptEdgeConfs,
    IpcUtil.genMainIpcHandleListener(saveScriptEdgeConfs)
  )
  ipcMain.handle(
    IpcChannelNameConst.findScriptEdgeConfs,
    IpcUtil.genMainIpcHandleListener(findScriptEdgeConfs)
  )
  ipcMain.handle(
    IpcChannelNameConst.removeScriptEdgeConfsByIds,
    IpcUtil.genMainIpcHandleListener(removeScriptEdgeConfsByIds)
  )
  ipcMain.handle(
    IpcChannelNameConst.removeScriptEdgeConfs,
    IpcUtil.genMainIpcHandleListener(removeScriptEdgeConfs)
  )
}

export type { TestConnType } from './host-conn-conf'

export type {
  ShellSessionOpenType,
  ShellSessionExecType,
  ShellSessionCloseType
} from './shell-session'

export type { SSHSessionOpenType, SSHSessionExecType, SSHSessionCloseType } from './ssh-session'
